/*
*  Copyright 2012 m4f10
*
*  This file is part of uScan.
*  uScan is free software: you can redistribute it and/or modify it under the
*  terms of the GNU General Public License as published by the 
*  Free Software Foundation, either version 3 of the License, or (at your option)
*  any later version.
*
*    uScan is distributed in the hope that it will be useful, 
*    but WITHOUT ANY WARRANTY; without even the implied warranty of 
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* 
*    See the GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License 
*    along with uScan. If not, see http://www.gnu.org/licenses/.
*/
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uscan;

/**
 *
 * @author m4f10
 */
public class UScanMain extends Thread {
    
    protected UScanProgress     prog;
    protected UScanCore         core;
    public  Main              form;
    private String          result;
    protected UScanLog           log;
    protected UScanSettings settings;
    private boolean isScanStarted;
    
    public UScanMain(Main form)
    {
        int mult = 0;
        
        // Init core
        this.core = new UScanCore();
        this.core.setDictionnary("./dicts.xml");
        this.core.setTargetURI(form.tAddress.getText());
        this.settings = form.settings;
        this.result = "";
        // Save Form to local class
        this.form = form;
        this.log = new UScanLog(this);
//        this.settings = form.settings;
        // Init progress
        if(form.cRegular.isSelected())
        {
            mult++;
        }
        if(form.cValue.isSelected())
        {
            mult++;
        }
        if(form.cArray.isSelected())
        {
            mult++;
        }
        this.prog = new UScanProgress(form, this.core.getDictionnary().length*mult);
        
        
        
        // Linking progress to core
        this.core.setUScanProgress(prog);
        // Linking error handling to core
        this.core.setUScanLog(this.log);
        // Linking settings to core
        this.core.setUScanSettings(this.settings);
        
    }
    
    public void killThread()
    {
        this.core.killThreads();
        this.stop();
    }
    
    public void launchUScan()
    {
        Thread thread = this;
        this.start();
    }
    
    @Override
    public void run()
    {
        prog.startMonitoring();
        log.clearLog();
        if(this.form.cRegular.isSelected())
        {
            String[] rslt;
            rslt = core.GETScan();
            if(rslt!=null && rslt[0]!=" ")
            {
                this.result = "Parameters found with regular method :\n";
                for(int i=0; i<rslt.length; i++)
                {
                    this.result += rslt[i]+"\n";
                }
            }
            else
            {
                this.result = "No parameters found with regular method :(\n";
                this.prog.forceFinish();
            }
            this.form.repaint();
        }
        
        if(this.form.cValue.isSelected())
        {
            String[] rslt;
            rslt = this.core.GETWithValueScan();
            if(rslt!=null && rslt[0]!=" ")
            {
                result += "Parameters found with Value Injection method :\n";
                for(int i=0; i<rslt.length; i++)
                {
                    result += rslt[i]+"\n";
                }
            }
            else
            {
                result += "No parameters found with Value Injection method :(\n";
                this.prog.forceFinish();
            }
            this.form.repaint();
        }
        
        if(this.form.cArray.isSelected())
        {
            String[] rslt;
            rslt = this.core.GETWithArrayScan();
            if(rslt!=null && rslt[0]!=" ")
            {
                result += "Parameters found with Array Injection method :\n";
                for(int i=0; i<rslt.length; i++)
                {
                    result += rslt[i]+"\n";
                    
                }
            }
            else
            {
                result += "No parameters found with Array Injection method :(\n";
                this.prog.forceFinish();
            }
        }
        
        this.form.tResult.append("Finished scanning!\n");
        
        this.form.bScan.setText("Scan!");
        this.form.isScanStarted = false;
        
        //this.form.tResult.setText(result);
        
    }
   
}
